from django.db import models
from django.contrib.auth.models import User

class Character(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)
    level = models.IntegerField(default=1)
    gold = models.IntegerField(default=0)
    base_hp = models.IntegerField(default=100)
    base_attack = models.IntegerField(default=10)
    base_defense = models.IntegerField(default=5)
    created_at = models.DateTimeField(auto_now_add=True)
    
    # 游戏统计数据
    highest_floor = models.IntegerField(default=0)
    monsters_killed = models.IntegerField(default=0)
    total_gold_earned = models.IntegerField(default=0)
    games_played = models.IntegerField(default=0)

    @property
    def total_hp(self):
        """计算包含被动技能效果的总生命值"""
        base_hp = self.base_hp
        # 获取强壮技能
        try:
            strong_skill = self.characterskill_set.get(
                skill__name='强壮',
                skill__skill_type='PASSIVE'
            )
            # 每级增加10%生命值
            hp_bonus = base_hp * (strong_skill.current_level * 0.1)
            return int(base_hp + hp_bonus)
        except CharacterSkill.DoesNotExist:
            return base_hp

class Skill(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField()
    skill_type = models.CharField(max_length=20, choices=[
        ('ACTIVE', '主动技能'),
        ('PASSIVE', '被动技能')
    ])
    effect_type = models.CharField(max_length=20, choices=[
        ('DAMAGE', '伤害'),
        ('HEAL', '治疗'),
        ('BUFF', '增益'),
        ('DEBUFF', '减益')
    ])
    base_value = models.IntegerField(help_text='基础效果值')
    level_value = models.IntegerField(help_text='每级增加效果值')
    base_level = models.IntegerField(default=1)
    max_level = models.IntegerField(default=5)
    cooldown = models.IntegerField(default=0, help_text='冷却回合数')
    cost = models.IntegerField(default=100, help_text='学习技能的金币花费')

    def __str__(self):
        return self.name

class Monster(models.Model):
    name = models.CharField(max_length=50)
    monster_type = models.CharField(max_length=20, choices=[
        ('NORMAL', '普通怪'),
        ('ELITE', '精英怪'),
        ('BOSS', 'Boss')
    ])
    hp = models.IntegerField()
    attack = models.IntegerField()
    defense = models.IntegerField()
    reward_gold = models.IntegerField()
    image = models.CharField(max_length=100, default='monster/default.png')

    def __str__(self):
        return self.name

class GameSession(models.Model):
    character = models.ForeignKey(Character, on_delete=models.CASCADE)
    current_floor = models.IntegerField(default=1)
    current_hp = models.IntegerField()
    is_active = models.BooleanField(default=True)
    started_at = models.DateTimeField(auto_now_add=True)
    ended_at = models.DateTimeField(null=True, blank=True)
    current_monsters = models.ManyToManyField(Monster, related_name='game_sessions')


class CharacterSkill(models.Model):
    character = models.ForeignKey(Character, on_delete=models.CASCADE)
    skill = models.ForeignKey(Skill, on_delete=models.CASCADE)
    current_level = models.IntegerField(default=1)
    is_equipped = models.BooleanField(default=False)
    cooldown_remaining = models.IntegerField(default=0)

    @property
    def is_in_cooldown(self):
        return self.cooldown_remaining > 0
    
    @property
    def remaining_cooldown(self):
        return self.cooldown_remaining

class Floor(models.Model):
    game_session = models.ForeignKey(GameSession, on_delete=models.CASCADE)
    floor_number = models.IntegerField()
    floor_type = models.CharField(max_length=20, choices=[
        ('BATTLE', '战斗'),
        ('SHOP', '商店'),
        ('TREASURE', '宝藏'),
        ('REST', '休息点')
    ])
    is_completed = models.BooleanField(default=False)

class Item(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField()
    item_type = models.CharField(max_length=20, choices=[
        ('WEAPON', '武器'),
        ('ARMOR', '防具'),
        ('POTION', '药水'),
        ('SCROLL', '卷轴')
    ])
    price = models.IntegerField()
    effect_value = models.IntegerField()

class Shop(models.Model):
    floor = models.OneToOneField(Floor, on_delete=models.CASCADE)
    items = models.ManyToManyField(Item, through='ShopItem')

class ShopItem(models.Model):
    shop = models.ForeignKey(Shop, on_delete=models.CASCADE)
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    quantity = models.IntegerField(default=1)
    is_sold = models.BooleanField(default=False)

class MonsterSkill(models.Model):
    monster = models.ForeignKey(Monster, on_delete=models.CASCADE)
    skill = models.ForeignKey(Skill, on_delete=models.CASCADE)
    skill_level = models.IntegerField(default=1)

    class Meta:
        unique_together = ('monster', 'skill') 